/**
 * Copyright 2015-2016 eyaomall.cn
 * All rights reserved.
 *
 * @project
 * @author jackey
 * @version 1.0
 * @date 2016-01-06
 */
package com.eyaomall.wxmall.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.eyaomall.wxmall.common.constants.SmsConstants;
import com.eyaomall.wxmall.common.error.BECode;
import com.eyaomall.wxmall.common.helper.MemberHelper;
import com.eyaomall.wxmall.model.po.Member;
import com.eyaomall.wxmall.model.po.Shop;
import com.eyaomall.wxmall.model.pojo.ResFlag;
import com.eyaomall.wxmall.service.MemberService;
import com.eyaomall.wxmall.service.OrderService;
import com.eyaomall.wxmall.service.ShopService;
import com.eyaomall.wxmall.service.SmsService;
import com.github.pagehelper.StringUtil;
import com.lycheeframework.core.cmp.api.http.GsnData;
import com.lycheeframework.core.cmp.api.http.GsnResponse;
import com.eyaomall.wxmall.common.util.ExpressUtils;
import com.lycheeframework.core.exception.ErrorCodeException;

/**
 * 会员controller
 * @author pro
 *
 */
@Controller
@RequestMapping("member")
public class MemberController {
	
	@Autowired
	private MemberService memberService;
	
	@Autowired
	private ShopService shopService;
	
	@Autowired
	private OrderService orderService;
	
	@Autowired
	private SmsService smsService;

	/**
	 * 会员中心
	 * @param openId
	 * @return
	 */
	@RequestMapping("index")
	public String index(HttpServletRequest request,Map<String,Object> data){
		Shop shop = shopService.queryById(MemberHelper.getShopId());
		
		String nickName = MemberHelper.getNickName();
		data.put("nickName", nickName);
		data.put("phone", MemberHelper.getPhone());
		data.put("headImg",MemberHelper.getHeadImg());
		data.put("appId", shop.getWxAppId());
		data.put("shop", shop);
		
		data.put("calc", orderService.calc(MemberHelper.getId()));
		
		return "member/index";
	}
	
	/**
	 * 绑定手机号码
	 * @return
	 */
	@RequestMapping("bind")
	public String bind(String redirect,Map<String,Object> data){
		data.put("redirectUrl", redirect);
		
		return "member/bind";
	}
	
	/**
	 * 绑定手机
	 * @param phone
	 * @return
	 */
	@RequestMapping("bind_phone")
	public @ResponseBody GsnData bindPhone(String phone,String code){
		if(ExpressUtils.isNotPhone(phone))
			throw new ErrorCodeException(BECode.global_900000);
		
		if(StringUtil.isEmpty(code))
			throw new ErrorCodeException(BECode.member_100002);
		
		smsService.checkSmsCode(phone, code, SmsConstants.CHANGE_PHONE);
		
		Member member = memberService.queryById(MemberHelper.getOpenId(),MemberHelper.getShopId());
		member.setPhone(phone);
		
		return GsnResponse.toData(new ResFlag(memberService.update(member)));
	}
	
	/**
	 * 发送短信码
	 * @param phone
	 * @return
	 */
	@RequestMapping("send_sms")
	public @ResponseBody GsnData sendBindSms(String phone){
		if(ExpressUtils.isNotPhone(phone))
			throw new ErrorCodeException(BECode.global_900000);
		
		Member member = new Member();
		member.setPhone(phone);
		member.setShopId(MemberHelper.getShopId());
		member = memberService.query(member);
		if(member != null){
			throw new ErrorCodeException(BECode.member_100001);
		}
		smsService.inTie(phone);
		
		return GsnResponse.toData(true);
	}
}
